﻿using MinecraftClient;
using MinecraftClient.Mapping;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text.RegularExpressions;
using MinecraftClient.Commands;

namespace MinecraftClient.ChatBots.MyBots
{
    public class GoldCraft : ChatBot
    {
        private Location facing;
        private int timerDefaut = 400;
        private int sleepTimerDefault = 200;
        private int timer = 200;
        private int sleepTimer = 50;
        private bool isSleep = false;
        private bool enable = false;
        public override void Initialize()
        {
            double x = GetCurrentLocation().X - 1;
            double y = GetCurrentLocation().Y;
            double z = GetCurrentLocation().Z;
            facing = new Location(x, y, z);
            
            
            PerformInternalCommand("ac reload");
            LogToConsole("GoldCraft loaded.");
        }

        public override void Update()
        {
            if (!enable) return;
            if (isSleep)
            {
                if (sleepTimer > 0) sleepTimer--;
                else
                {
                    PerformInternalCommand("ac start gold");
                    PerformInternalCommand("adt off");
                    timer = timerDefaut;
                    isSleep = false;
                }
                return;
            }

            if (timer > 0) timer--;
            else
            {
                PerformInternalCommand("ac stop");
                PerformInternalCommand("adt on");
                sleepTimer = sleepTimerDefault;
                isSleep = true;
            }

        }

        public override void GetText(string text)
        {
            text = GetVerbatim(text);
            Regex start = new Regex(@"^\[.+\]<Gare_TH> goldcraft start$");
            Regex stop = new Regex(@"^\[.+\]<Gare_TH> goldcraft stop$");
            Regex off = new Regex(@"^\[.+\]<Gare_TH> goldcraft off$");
            if (start.IsMatch(text))
            {
                Start();
                return;
            }

            if (stop.IsMatch(text))
            {
                Stop();
                return;
            }

            if (off.IsMatch(text))
            {
                Off();
            }
        }

        private void Off()
        {
            PerformInternalCommand("dsb unload 3");
        }

        private void Stop()
        {
            enable = false;
            SendText("Stopping Script.");
        }

        private void Start()
        {
            enable = true;
            PerformInternalCommand("ac start gold");
            LookAtLocation(facing);
            PerformInternalCommand("adt on");
            SendText("Starting script.");
        }

    }
}